package com.lanou.controller;

import com.lanou.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author lizhongren1.
 */
@WebServlet(urlPatterns = "/login")
public class LoginServlet extends HttpServlet {

    private UserService userService = new UserService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        String username = req.getParameter("username");

        String password = req.getParameter("password");

        // 假设用户名和密码都是root
        // 具体使用数据库进行判断
        if (userService.login(username, password)){
            // 登录成功
            HttpSession session = req.getSession();
            session.setAttribute("isLogin",username);
            resp.sendRedirect("/home");
        } else {
            // 登录失败
            // 转到注册页面
            // 转到错误页面
            req.getRequestDispatcher("/error.html").forward(req,resp);
        }
    }
}
